package cn.dglydrpy.study.smartmvc.controller;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import cn.dglydrpy.study.smartmvc.base.annotation.RequestMapping;


/*
 * 业务控制器
 */
public class BizController {
	
	@RequestMapping("/hello.do")
	public String hello(){
		System.out.println("Hello.do");
		return "hello";//返回视图名
	}
	
	@RequestMapping("/toLogin.do")
	public String toLogin(){
		return "login";
	}
	@RequestMapping("/login.do")
	public String login(HttpServletRequest request){
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if ("Tom".equals(username) && "123456".equals(password)) {
			//重定向:
			return "redirect:toList.do";
		}else{
			//转发
			request.setAttribute("msg","login_failed");
			return "login";
		}
	}
	@RequestMapping("/toList.do")
	public String toList(){
		return "listUser";
	}
	
	/**
	 * 负责处理 /emp/list.do 显示员工列表
	 * 在Web程序运行期间， 将/emp/list.do请求映射到
	 * list() 方法上，也就是请求/emp/list.do时候，
	 * 执行list()方法
	 */
	@RequestMapping("/test/list.do")
	public String list(HttpServletRequest request) {
		return "list";
	}
	/**
	 * 负责处理 /emp/add.do 显示添加员工界面
	 */
	@RequestMapping("/test/add.do")
	public String add(HttpServletRequest request) {
		return "add";
	}
	
	@RequestMapping("/test/hello.do")
	public String hello(HttpServletRequest request) {
		request.setAttribute("msg", "HI"); 
		return "hello";
	}
	// ...
	
	@RequestMapping("/test/test.do")
	public String test(HttpServletRequest request) {
		//测试重定向功能
		String path=request.getContextPath()+
				"/emp/list.do";
		return "redirect:"+path;
	}
	
}
